package com.teradyne.merlin2.ui.client.activities;

import java.util.ArrayList;
import java.util.List;

import com.google.gwt.activity.shared.AbstractActivity;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.place.shared.PlaceController;
import com.google.gwt.user.client.ui.AcceptsOneWidget;
import com.teradyne.merlin2.ui.client.places.HomePlace;
import com.teradyne.merlin2.ui.client.places.OrderListPlace;
import com.teradyne.merlin2.ui.client.places.QuotePlace;
import com.teradyne.merlin2.ui.client.places.WorklistPlace;
import com.teradyne.merlin2.ui.client.views.TopMenuView;

public class TopMenuActivity extends AbstractActivity implements
		TopMenuView.Presenter {

	private final TopMenuView view;
	private final PlaceController placeController;

	private List<HandlerRegistration> handlerRegistration = new ArrayList<HandlerRegistration>();

	public TopMenuActivity(TopMenuView view, PlaceController placeController) {
		this.view = view;
		this.placeController = placeController;
	}

	@Override
	public void start(AcceptsOneWidget container, EventBus eventBus) {
		container.setWidget(view.asWidget());
		view.setPresenter(this);
		events(eventBus);
		bind();
	}

	private void events(EventBus eventBus) {

	}

	private void bind() {
		handlerRegistration.add(view.getBtnHome().addClickHandler(
				new ClickHandler() {

					@Override
					public void onClick(ClickEvent event) {
						placeController.goTo(new HomePlace());
					}
				}));

		handlerRegistration.add(view.getBtnOrder().addClickHandler(
				new ClickHandler() {

					@Override
					public void onClick(ClickEvent event) {
						placeController.goTo(new OrderListPlace());
					}
				}));

		handlerRegistration.add(view.getBtnQuote().addClickHandler(
				new ClickHandler() {

					@Override
					public void onClick(ClickEvent event) {
						placeController.goTo(new QuotePlace());
					}
				}));

		handlerRegistration.add(view.getBtnWorklist().addClickHandler(
				new ClickHandler() {

					@Override
					public void onClick(ClickEvent event) {
						placeController.goTo(new WorklistPlace());
					}
				}));
	}

	@Override
	public void onStop() {
		for (HandlerRegistration registration : handlerRegistration) {
			registration.removeHandler();
		}

		handlerRegistration.clear();
	}

}